<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆编号" prop="carNo">
        <el-input
          v-model.trim="queryParams.carNo"
          placeholder="请输入车辆编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model.trim="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="teamList" max-height="430">
      <el-table-column label="车辆编号" align="center" prop="carNumber" />
      <el-table-column label="车牌照" align="center" prop="carLicensePlate" />
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="司机电话" align="center" prop="remdriverPhoneark" />
      <el-table-column label="工作状态" align="center" prop="workingState" />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:team:edit']"
          >回单</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排班管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item :rules="[{ required: true, message: '请选择', trigger: 'blur' }]" label="选择班组" prop="teamId">
            <div style="display:flex;gap: 20px;align-items:center">
                <span v-if="form.teamInfo">{{form.teamInfo.teamName}}</span>
                <el-button type="text" @click="dialogVisible = true">{{form.teamId ? '请重新选择' : '请选择'}}</el-button>
            </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <SelectTeamDialog
        :dialogVisible="dialogVisible"
        @onClose="dialogVisible = false"
        @onSubmit="selectTeam"
    ></SelectTeamDialog>
  </div>
</template>

<script>
import { listTeam, getTeam, delTeam, addTeam, updateTeam } from "@/api/service/production/scheduling";
import { getReplyList, replyConcrete } from "@/api/service/production/receiptManagement";
import SelectTeamDialog from "@/components/SelectDialog/SelectTeamDialog"
export default {
    components: {
        SelectTeamDialog
    },
  name: "Team",
  data() {
    return {
        dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排班管理表格数据
      teamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        driverName: null,
        carNo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  computed:{

  },
  created() {
    this.getList();
  },
  methods: {
    selectTeam(row){
        
        this.form.teamInfo = row
        this.form.teamId = row.teamId
    },
    /** 查询排班管理列表 */
    getList() {
      this.loading = true;
      getReplyList(this.queryParams).then(response => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sortTeamId: null,
        teamId: null,
        teamInfo: null,
        executeTeamStartTime: null,
        executeTeamEndTime: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sortTeamId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排班管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
        this.$modal.confirm('确定回单吗？').then(function() {
            return replyConcrete({
                ids: row.concreteId,
                status: 1
            });
        }).then(() => {
            this.getList();
            this.$modal.msgSuccess("回单成功");
        }).catch(() => {});

    //   this.reset();
    //   const sortTeamId = row.sortTeamId || this.ids
    //   getTeam(sortTeamId).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改排班管理";
    //   });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sortTeamId != null) {
            updateTeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sortTeamIds = row.sortTeamId || this.ids;
      this.$modal.confirm('是否确认删除排班管理编号为"' + sortTeamIds + '"的数据项？').then(function() {
        return delTeam(sortTeamIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/team/export', {
        ...this.queryParams
      }, `team_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
